﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MovieRental.Entities;
using MovieRental.BuisnessComponents;
using MovieRental.EnhancedEntities;
using System.ComponentModel;
using System.Linq;
using MovieRental_Presentation.DataViews;
using MovieRental_Presentation.Helpers;

namespace MovieRental_Presentation
{
	public partial class CustomerDetails
	{
        private CustomerDetailsDataView _dataView;

		public CustomerDetails(Guid customerId, bool readOnly = true)
		{
			this.InitializeComponent();
            _dataView = new CustomerDetailsDataView(customerId);
            DataContext = _dataView;
            _dataView.IsReadOnly = readOnly;
		}

		private void backButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            this.NavigationService.GoBack();
		}

        private void editButton_Click(object sender, RoutedEventArgs e)
        {
            _dataView.IsReadOnly = false;
        }

        private void cancelEditButton_Click(object sender, RoutedEventArgs e)
        {
            _dataView.IsReadOnly = true;
            _dataView.Customer = new Customer(ComponentManager.Instance.CustomerComponent.Get(_dataView.Customer.Entity.Id));
        }
		
		private bool ValidateInput()
		{
            return (firstNameTextBox.Validate() &&
                    lastNameTextBox.Validate() &&
                    emailTextBox.Validate() &&
                    phoneNoTextBox.Validate());
		}

        private void applyButton_Click(object sender, RoutedEventArgs e)
        {
			if (!ValidateInput())
				return;
            ComponentManager.Instance.CustomerComponent.AddOrUpdate(_dataView.Customer.Entity);
            _dataView.IsReadOnly = true;
        }

        private void returnButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new ReturnDialog(loansDataGrid.SelectedItems.OfType<Loan>()));
        }

        private void dashboardButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	NavigationService.Navigate(new Dashboard());
        }

        private void customersButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	NavigationService.Navigate(new Customers());
        }

        private void moviesButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	NavigationService.Navigate(new Movies());
        }

        private void statisticsButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	NavigationService.Navigate(new Statistics());
        }

        private void returnAllButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	NavigationService.Navigate(new ReturnDialog(loansDataGrid.ItemsSource.OfType<Loan>()));
        }

        private void assignButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	var cart = (App.Current as App).CartDataView.CurrentCart.Entity;
            ComponentManager.Instance.LoanCartComponent.AssignCustomerToCart(cart, _dataView.Customer.Entity);
        }

        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            Customer customer = _dataView.Customer;
            var globalResources = (Application.Current as App).Resources;
            if (!ComponentManager.Instance.CustomerComponent.CanDelete(customer.Entity))
            {
                string message = string.Format((string)globalResources["deleteCustomerFailedMessage"], customer.FullName, customer.NotReturnedEditions);
                MessageBox.Show(message, "Can't delete the customer.", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }
            var result = MessageBox.Show("Do you really want do delete this customer?", "Delete", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.Yes)
                ComponentManager.Instance.CustomerComponent.Delete(customer.Entity);
            NavigationService.GoBack();
        }

    }
}